

package com.lzx.iteam.widget;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint.FontMetrics;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.EditText;


public class DialpadEditText extends EditText {

    private static final String TAG = "DialpadEditText";
    
    boolean first = true;
    Canvas canvas = new Canvas();
    
    public DialpadEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        //init();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (MotionEvent.ACTION_UP == event.getAction()) {
            Log.w(TAG, "DialpadEditText.onTouchEvent return... ");
            return true;
        }
        return super.onTouchEvent(event);
    }
 
    public void init(){
        TextPaint paint = getPaint();
        float textsize = paint.getTextSize();
        int height = getHeight()-getPaddingBottom();
        int width = getWidth()-getPaddingLeft()-getPaddingRight();
        if(height<=0||width<=0){
            return;
        }
        while(true){
            FontMetrics fm = paint.getFontMetrics();
            int fontheight = (int) Math.ceil(fm.descent - fm.ascent);
            if(fontheight<height){
                textsize+=0.5;
                paint.setTextSize(textsize);
                float textwidth = paint.measureText(getText().toString());
                if(textwidth>width){
                    textsize-=0.5;
                    paint.setTextSize(textsize);
                    break;
                }
                continue;
            }else{
                break;
            }
        }
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
    
    public void resizeText(String text){
        int width = getWidth()-getPaddingLeft()-getPaddingRight();
        if(width>0){
            TextPaint paint = getPaint();
            float textsize = paint.getTextSize();
            int textWidth = (int) paint.measureText( text);
            if(textWidth<=width){
                init();
                return;
            }else{
                while(true){
                    textsize-=0.5;
                    paint.setTextSize(textsize);
                    textWidth = (int) paint.measureText(text);
                    if(textWidth<=width){
                        break;
                    }
                }
            }
        }
   
    }
    
    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        if(text.toString().length()==0){
            init();
        }
        resizeText(text.toString());
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if(w!=oldw){
            resizeText(getText().toString());
        }
    }
}
